package com.wawu.server.controller;

import com.wawu.common.annotation.IOC.Autowired;
import com.wawu.common.annotation.IOC.component.RestController;
import com.wawu.common.annotation.controller.mapping.DeleteMapping;
import com.wawu.common.annotation.controller.mapping.GetMapping;
import com.wawu.common.annotation.controller.mapping.PostMapping;
import com.wawu.common.annotation.controller.mapping.RequestMapping;
import com.wawu.common.annotation.controller.parameter.RequestBody;
import com.wawu.common.annotation.controller.parameter.RequestParam;
import com.wawu.common.result.Result;
import com.wawu.pojo.dto.AddTypeDTO;
import com.wawu.pojo.dto.GoodDTO;
import com.wawu.pojo.dto.LoginDTO;
import com.wawu.pojo.entity.GoodDetail;
import com.wawu.pojo.entity.GoodType;
import com.wawu.pojo.query.OrderQuery;
import com.wawu.pojo.vo.GetGoodsInfoByGoodsIdVO;
import com.wawu.pojo.vo.GetOrders.GetOrderVO;
import com.wawu.pojo.vo.GoodVO;
import com.wawu.pojo.vo.LoginVO;
import com.wawu.pojo.vo.UserVO;
import com.wawu.server.service.AdminService;
import com.wawu.server.service.MallService;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    /**
     * 管理员端登录
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) throws SQLException {
        return Result.success(adminService.login(loginDTO));
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @GetMapping("/allUser")
    public Result<List<UserVO>> getAllUser() throws SQLException, InvocationTargetException, IllegalAccessException {
        return Result.success(adminService.getAllUser());
    }

    @GetMapping("/getType")
    public Result<List<GoodType>> getGoodTypes() throws SQLException {
        return Result.success(adminService.getGoodTypes());
    }

    /**
     * 添加商品类型
     * @param addTypeDTO
     * @return
     */
    @PostMapping("/addType")
    public Result addGoodType(@RequestBody AddTypeDTO addTypeDTO) throws SQLException {//这里只有一个属性，这里可以根据框架优化
        adminService.addGoodType(addTypeDTO.getName());
        return Result.success();
    }

    //TODO 看看吧：这里使用这个平替下
    @Autowired
    MallService mallService;
    /**
     * 通过商品类型id获取商品
     * @param typeId
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @GetMapping("/getGoodsByType")
    public Result<List<GoodVO>> getGoodVOsByTypeId(@RequestParam Long typeId) throws SQLException, InvocationTargetException, IllegalAccessException {
        return Result.success(mallService.getGoodVOsByTypeId(typeId));
    }

    /**
     * 添加商品
     * @param goodDTO
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws SQLException
     */
    @PostMapping("/addGoods")
    public Result addGood(@RequestBody GoodDTO goodDTO) throws InvocationTargetException, IllegalAccessException, SQLException {
        adminService.addGood(goodDTO);
        return Result.success();
    }

    /**
     * 更新商品
     * @param goodDTO
     * @return
     */
    @PostMapping("/updateGoods")
    public Result updateGood(@RequestBody GoodDTO goodDTO) throws InvocationTargetException, IllegalAccessException {
        adminService.updateGood(goodDTO);
        return Result.success();
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/deleteGoods")
    public Result deleteGoodById(@RequestParam Long id) throws SQLException {
        adminService.deleteGoodById(id);
        return Result.success();
    }

    /**
     * 获取商品详情
     * @param id
     * @return
     */
    @GetMapping("/getGoodsInfo")
    public Result<GetGoodsInfoByGoodsIdVO> getGoodsInfoByGoodsId(@RequestParam Long id) throws SQLException {
        return Result.success(adminService.getGoodsInfoByGoodsId(id));
    }


    /**
     * 添加商品详情（规格）
     * @param goodDetail
     * @return
     * @throws SQLException
     */
    @PostMapping("/addSpec")
    public Result<GoodDetail> addSpec(@RequestBody GoodDetail goodDetail) throws SQLException {
        return Result.success(adminService.addGoodDetail(goodDetail));
    }


    //TODO 后续有时间添加解析时间类型
    //TODO 带完善：删除商品可能会出现异常，这里需要改一下

    /**
     * 动态查询订单
     * @param userId
     * @param state
     * @param minAmount
     * @param maxAmount
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/orders")
    public Result<List<GetOrderVO>> getOrders(
            @RequestParam("userId") Long userId,
            @RequestParam("state") Integer state,
            @RequestParam("minAmount") Double minAmount,
            @RequestParam("maxAmount") Double maxAmount,
            @RequestParam("startDate") LocalDateTime startDate,
            @RequestParam("endDate")  LocalDateTime endDate
    ) throws SQLException, InvocationTargetException, IllegalAccessException {
//        System.out.println(userId);
//        System.out.println(state);
//        System.out.println(minAmount);
//        System.out.println(maxAmount);
//        System.out.println(startDate);
//        System.out.println(endDate);
        return Result.success(adminService.getOrders(
                OrderQuery.builder()
                        .userId(userId)
                        .state(state==null||state == -1 ?null:state)
                        .minAmount(minAmount)
                        .maxAmount(maxAmount)
                        .startDate(startDate)
                        .endDate(endDate)
                        .build()
        ));
    }


}
